﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MicrobloggingInterface;

namespace HexunMicrobloggingClientUI
{
    public class CoreFunc
    {
        public static bool Send(string userName, string password, string textContent, 
                                out string responseType, out string responseContent, out string errorMessage)
        {
            return IssueMicroblogging(userName, password, textContent, out responseContent, out responseType, out errorMessage);
        }

        /// <summary>
        /// Issue a microblogging message.
        /// </summary>
        /// <param name="userName">User name of account.</param>
        /// <param name="password">Passwor of account.</param>
        /// <param name="content">Message content.</param>
        /// <param name="errorMessage">Error message if failed.</param>
        /// <returns>If the mmicroblogging message was sent successfully or not.</returns>
        private static bool IssueMicroblogging(string userName, string password, string content,
                                               out string responseContent, out string responseType,
                                               out string errorMessage)
        {
            //Dictionary<string, string> assambliesInfo = SearchAssamblies();

            responseContent = string.Empty;
            responseType = string.Empty;
            errorMessage = string.Empty;

            IMicroblogging mb = null;
            MicrobloggingFactory factory = new MicrobloggingFactory();
            mb = factory.GetMicrobloggingType("HexunMicroblogging", "HexunMicroblogging.HexunMBImp");
            
            if (mb == null)
            {
                errorMessage = "factory.GetMicrobloggingType failed.";
                return false;
            }

            if (!mb.Issue(userName, password, content))
            {
                errorMessage = mb.GetLastError();
                return false;
            }

            // Parse successful resopnse content.
            //responseContent = mb.GetResponseContent();
            //responseType = mb.GetResponseType().ToString();

            return true;
        }

        private static Dictionary<string, string> SearchAssamblies()
        {
            Dictionary<string, string> assambliesInfo = new Dictionary<string, string>();

            assambliesInfo.Add("HexunMicroblogging", "HexunMicroblogging.HexunMBImp");

            return assambliesInfo;
        }
    }
}
